<?php

namespace App\V3\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterielsTypeModel extends Model
{
    use HasFactory;

    protected $table = 'materiel_type';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //商品编号查重-修改
    static public function checkMaterielTypeCode($data)
    {
        return SELF::where([
            'materiel_no' => $data['materiel_no'],
            'main_id' => $data['main_id'],
            'delete_flag' => 0,
        ])->where('id', '!=', $data['id'])->first();
    }

    static public function checkMaterielTypeRandCode($data)
    {
        return SELF::where([
            'materiel_no' => $data['materiel_no'],
            'main_id' => $data['main_id'],
            'delete_flag' => 0
        ])->first();
    }

    //商品信息查重
    static public function checkMaterielInfo($data)
    {
        return SELF::where([
            'materiel_name' => $data['materiel_name'],
            'type_name' => $data['type_name'],
            'main_id' => $data['main_id'],
            'delete_flag' => 0,
        ])
            ->first();
    }

    //获取指定商品详细信息
    static public function info($id)
    {
        return SELF::select(
            'a.id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_img',
            'a.materiel_no',
            'a.materiel_class_id',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.lower_selling_price',
            'a.is_album',
            'a.materiel_unit',
            'a.lower_selling_price',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.is_use_serial'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where('a.id', $id)
            ->where('a.delete_flag', 0)
            ->first();
    }

    //物料商品列表
    static public function materielTypeSelect($mainId, $keyword, $condition, $pageSize, $currentPage)
    {
        return SELF::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.is_use_serial',
            'a.materiel_unit'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where($condition)
            ->where(function($query) use ($keyword) {
                $query->orWhere('a.materiel_name', 'like', '%'.$keyword.'%');
                $query->orWhere('a.type_name', 'like', '%'.$keyword.'%');
                $query->orWhere('a.materiel_no', 'like', '%'.$keyword.'%');
                $query->orWhere('b.class_name', 'like', '%'.$keyword.'%');
                $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
            })
            ->where('a.delete_flag', 0)
            ->where('a.main_id', $mainId)
            ->orderBy('a.id', 'DESC')
            ->paginate($pageSize,['*'], '',$currentPage);
    }

    //物料商品列表-含库存信息
    //物料类型列表
    static public function getMaterielTypeList($parentId, $condition, $keyword, $data)
    {
        return StorageManageModel::select(
            'b.*',
            'c.id as s_class_id',
            'c.class_name as s_class_name',
            'c.class_no as s_class_no',
            'd.class_name as f_class_name',
            'd.class_no as f_class_no',
            'd.id as f_class_id',
            'a.in_storage_num',
            'a.storage_id',
            'e.storage_name',
            'b.length',
            'b.width',
            'b.height',
            'b.weight',
            'b.attribute',
            'b.cost',
            'a.shelf_id',
            'a.location_id',
            's.shelf_name',
            'l.location_name',
        )
            ->from('storage_manage as a')
            ->leftJoin('materiel_type AS b', 'b.id', '=', 'a.type_id')
            ->leftJoin('materiel_class AS c', 'c.id', '=', 'b.materiel_class_id')
            ->leftJoin('materiel_class AS d', 'd.id', '=', 'c.parent_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage_shelf as s','a.shelf_id','=','s.id')
            ->leftJoin('storage_shelf_location as l','a.location_id','=','l.id')

            ->where($parentId)
            ->where('a.delete_flag', 0)
            ->where('b.delete_flag', 0)
            ->where('e.delete_flag', 0)
            ->where($condition)
            ->where(function($query) use ($keyword) {
                $query->orWhere('b.materiel_name', 'like', '%'.$keyword.'%');
                $query->orWhere('b.type_name', 'like', '%'.$keyword.'%');
                $query->orWhere('b.materiel_no', 'like', '%'.$keyword.'%');
                $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
                $query->orWhere('d.class_name', 'like', '%'.$keyword.'%');
            })
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }

    //获取物料商品信息
    static public function getTypeInfo($typeId, $mainId)
    {
        return SELF::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_no',
            'b.id as materiel_class_id',
            'b.class_name',
            'b.class_no'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->where([
                'a.id' => $typeId,
                'a.main_id' => $mainId,
                'a.delete_flag' => 0,
            ])
            ->first();
    }
}
